package org.Modulo1;

import java.io.File;

import android.os.Environment;
import android.support.v4.app.Fragment;
import android.widget.EditText;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Captura_Automatica extends Fragment {
	private static final int FOTO = 1;
	private static final String DIRECTORIO = Environment
			.getExternalStorageDirectory().toString() + "/Ancient/";
	private static final String pick = DIRECTORIO + "/ocr.jpg";
	private static boolean taken = false;
	protected static final String PHOTO_TAKEN = "photo_taken";
	
	private EditText text;
	
	@Override
	public View onCreateView(LayoutInflater li, ViewGroup vg, Bundle b){
		View vista = li.inflate(0, vg, false);
		Button bt = (Button) vista.findViewById(1);
		text = (EditText) vista.findViewById(2);
		bt.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//startCamara();
			}
		});
		
		return vista;
	}
	
	/*//Inicia la camara
	protected void startCamara(){
		File file = new File(pick);
		Uri outputfileuri = Uri.fromFile(file);
		
		final Intent cam_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		Mandamos la captura a /Ancient/
		cam_intent.putExtra(MediaStore.EXTRA_OUTPUT, outputfileuri);
		
		startActivityForResult(cam_intent, FOTO);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		if(requestCode == FOTO){
			fotoTomada();
		}
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putBoolean("FotoTomada", taken);
	}
	
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		if (savedInstanceState.getBoolean("FotoTomada")) {
			fotoTomada();
		}
	}

	protected void fotoTomada() {
		taken = true;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 4;
		
		Bitmap bitmap = BitmapFactory.decodeFile(pick,options);
		try{
			ExifInterface exif = new ExifInterface(pick);
			int exiforientation = exif.getAttributeInt(
									ExifInterface.TAG_ORIENTATION,
									ExifInterface.ORIENTATION_NORMAL);
			
			int rotate = 0;
			
			switch(exiforientation){
				case ExifInterface.ORIENTATION_ROTATE_90:
					rotate = 90;
					break;
				case ExifInterface.ORIENTATION_ROTATE_180:
					rotate = 180;
					break;
				case ExifInterface.ORIENTATION_ROTATE_270:
					rotate = 270;
					break;
			}
			
			if(rotate != 0){
				int ancho = bitmap.getWidth();
				int alto = bitmap.getHeight();
				
				Matrix mtx = new Matrix();
				mtx.preRotate(rotate);
				
				bitmap = Bitmap.createBitmap(bitmap, 0, 0, ancho, alto, mtx, false);
				
			}
			
			bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
					
			
			String textoReconocido = "TextodeFoto";
			
			textoReconocido = textoReconocido.replaceAll("[^a-zA-Z0-9]+", " ");
			
			System.out.println(textoReconocido+" "+DIRECTORIO);
			text.setText(textoReconocido);			
			
		}catch(Exception e){
			
		}
	}*/
}
